﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAO;

namespace BUS
{
    public class HoSoDaiLyBUS
    {
        #region 1.Retrieving
        public static List<HoSoDaiLyDTO> SellectHoSoDaiLy_All()
        {
            return HoSoDaiLyDAO.SelectAllHoSoDaiLyAll();
        }
        public static List<HoSoDaiLyDTO> SelectHoSOByID(String id)
        {
            return HoSoDaiLyDAO.SellectByID(id);
        }

        public static List<HoSoDaiLyDTO> SelecllHoSoDaiLyByLoaiDaiLyAndQuan(string maLoaiDaiLy, string maQuan)
        {
            //kiem tra cai da
            //if (QuanBUS.CheckIDQuanExits(maQuan) == true && LoaiDaiLyBUS.CheckIDLoaiDaiLyDTO(maLoaiDaiLy))
            //{
            return HoSoDaiLyDAO.SellectHoSoDaiLyByLoaiDaiLyAndMaQuan(maLoaiDaiLy, maQuan);
            //}
            //return null;
        }

        public static bool CheckMaHoSoDaiLyExits(string maHoSoDaiLy)
        {

            return HoSoDaiLyDAO.CheckMaHoSoDaiLyExits(maHoSoDaiLy);
        }
        #endregion
        #region 2.Inserting
        public static bool InsertHoSoDaiLy(HoSoDaiLyDTO hoSoDaiLyInsert)
        {

            if (hoSoDaiLyInsert.MaHoSoDaiLy == "")
            {

                return false;
            }
            if (HoSoDaiLyBUS.CheckMaHoSoDaiLyExits(hoSoDaiLyInsert.MaHoSoDaiLy) == true)
            {

                return false;
            }

            //// kiem tra so dai ly toi da tren 1 quan
            //List<SoDaiLyToiDaCuaMotQuanDTO> listDaiLyMax = new List<SoDaiLyToiDaCuaMotQuanDTO>();
            //listDaiLyMax = SoDaiLyToiDaCuaMotQuanDAO.GetSoDaiLyToiDa();
            //int soDaiLyToiDa = listDaiLyMax[0].SoDaiLyToiDa;

            //List<HoSoDaiLyDTO> hoSoDaiLyTheoQuan = new List<HoSoDaiLyDTO>();
            //hoSoDaiLyTheoQuan = HoSoDaiLyDAO.GetDaiLyTheoQuan(hoSoDaiLyInsert.MaQuan);
            //int soDaiLyHienTai = hoSoDaiLyTheoQuan.Count;
            //if (soDaiLyHienTai >= soDaiLyToiDa)
            //{
            //    return false;
            //}

            //// kiem tra so tien no toi da
            //List<LoaiDaiLyDTO> listLoaiDaiLy = new List<LoaiDaiLyDTO>();
            //listLoaiDaiLy = LoaiDaiLyDAO.SellectLoaiDaiLyByID(hoSoDaiLyInsert.MaLoaiDaiLy);

            //float tienNoMax = listLoaiDaiLy[0].TienNoToiDa;
            //if (hoSoDaiLyInsert.TienNo >= tienNoMax)
            //{
            //    return false;
            //}

            return HoSoDaiLyDAO.InsertHoSoDaiLy(hoSoDaiLyInsert);
        }
        #endregion
        #region 3.Deleting
        public static bool DeleteHoSoDaiLy(HoSoDaiLyDTO hoSo)
        {
            if (HoSoDaiLyBUS.CheckMaHoSoDaiLyExits(hoSo.MaHoSoDaiLy) == false)
            {

                return false;
            }
            return HoSoDaiLyDAO.DeleteHoSoDaiLy(hoSo);
        }
        #endregion
        #region 4.Updating
        public static bool UpdateHoSo(HoSoDaiLyDTO hoSo)
        {

            if (HoSoDaiLyBUS.CheckMaHoSoDaiLyExits(hoSo.MaHoSoDaiLy) == false)
            {

                return false;
            }
            return HoSoDaiLyDAO.UpdateHoSo(hoSo);
        }
        #endregion
        #region 5.Finding
        public static List<HoSoDaiLyDTO> FindHoSoDaiLy(TimKiemDaiLyDTO daiLy)
        {

            if (daiLy.NgayTiepNhanEnd == "  /  /")
            {
                daiLy.NgayTiepNhanEnd = String.Empty;
            }
            if (daiLy.NgayTiepNhanBegin == "  /  /")
            {
                daiLy.NgayTiepNhanBegin = String.Empty;
            }

            try
            {
                DateTime t = DateTime.Parse(daiLy.NgayTiepNhanBegin);
                if (t.Year < 1000)
                {
                    daiLy.NgayTiepNhanBegin = String.Empty;
                }
            }
            catch (System.Exception)
            {

                daiLy.NgayTiepNhanBegin = String.Empty;

            }
            try
            {
                DateTime t = DateTime.Parse(daiLy.NgayTiepNhanEnd);
                if (t.Year < 1000)
                {
                    daiLy.NgayTiepNhanEnd = String.Empty;
                }
            }
            catch (System.Exception)
            {
                daiLy.NgayTiepNhanEnd = String.Empty;

            }


            if (daiLy.TienNoMax == "")
            {
                daiLy.TienNoMax = String.Empty;
            }
            if (daiLy.TienNoMin == "")
            {
                daiLy.TienNoMin = String.Empty;
            }
            if (daiLy.TenDaiLy == "")
            {
                daiLy.TenDaiLy = String.Empty;
            }
            if (daiLy.MaQuan == "")
            {
                daiLy.MaQuan = String.Empty;
            }
            if (daiLy.MaLoaiDaiLy == "")
            {
                daiLy.MaLoaiDaiLy = String.Empty;
            }
            if (daiLy.MaHoSoDaiLy == "")
            {
                daiLy.MaHoSoDaiLy = String.Empty;
            }
            if (daiLy.EMail == "")
            {
                daiLy.EMail = String.Empty;
            }
            if (daiLy.DienThoai == "")
            {
                daiLy.DienThoai = String.Empty;
            }
            if (daiLy.DiaChi == "")
            {
                daiLy.DiaChi = String.Empty;
            }

            return HoSoDaiLyDAO.FindHoSoDaiLy(daiLy);
        }
        #endregion
    }
}
